# -*- coding: utf-8 -*-
# __author__ : "初一丶"
# __time__ : 2018/8/13 13:43
# __file__ : time_tools.py

import time
import datetime
import calendar


# 获取时间戳 1534139293.38
time.time()

# 获取当前时间 Mon Aug 13 13:45:25 2018
time.ctime()

# 获取前一天的时间 Sun Aug 12 14:09:05 2018
time.ctime(time.time() - 24 * 60 * 60)

# 返回time.struct_time类型的对象
# time.struct_time(tm_year=2018, tm_mon=8, tm_mday=13, tm_hour=5, tm_min=52, tm_sec=6, tm_wday=0, tm_yday=225, tm_isdst=0)
time.gmtime()       # utc时间
time.localtime()    # 本地时间


# 获取当前时间 Mon Aug 13 13:45:25 2018
time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())

# 获取当前时间 2018-08-13 13:46:50
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# 获取当前年月日 2018-08-13
datetime.date.today()

# 获取当前时间 精确到微秒 2018-08-13 13:55:28.150613
datetime.datetime.now()

# 转换成time.struct_time 格式
# time.struct_time(tm_year=2018, tm_mon=8, tm_mday=13, tm_hour=13, tm_min=56, tm_sec=50, tm_wday=0, tm_yday=225, tm_isdst=-1)
struct = datetime.datetime.now().timetuple()
struct.tm_year,  struct.tm_mon      # 输出 2018， 8

# 当前时间加10天  2018-08-23 13:59:24.762814
datetime.datetime.now() + datetime.timedelta(days=10)

# 10天前的日期
datetime.datetime.now() + datetime.timedelta(days=-10)

# 10小时前的日期
datetime.datetime.now() + datetime.timedelta(hours=-10)

# 120秒之后的日期
datetime.datetime.now() + datetime.timedelta(seconds=120)

"""
     时间形式之间的转换
"""
time_stamp = time.time()                            # 时间戳
local_time = time.localtime(time_stamp)             # 时间戳转struct_time类型的本地时间
utc_time = time.gmtime(time_stamp)                  # 时间戳转struct_time类型的utc时间

time_stamp = time.mktime(local_time)                # struct_time类型的本地时间转时间戳
time_stamp = calendar.timegm(utc_time)              # struct_time类型的utc时间转时间戳

time.ctime(time_stamp)                              # 时间戳转字符串 Mon Aug 13 14:09:50 2018
time.asctime(local_time)                            # struct_time类型的本地时间转字符串
time.asctime(utc_time)                              # struct_time类型的utc时间转字符串

time.strftime('%Y-%m-%d %H:%M:%S, %w', local_time)  # struct_time类型的本地时间转字符串：自定义格式 2018-08-13 14:13:13, 1
                                                    # %w:  今天在这周的天数，范围为[0, 6]，6表示星期天
print(time.strftime("%Y-%m-%d, %H:%M:%S, %w", utc_time))
time.strptime("2018-08-13 14:13:13, 1", "%Y-%m-%d %H:%M:%S, %w")    # 字符串转struct_time类型

a_datetime_local = datetime.datetime.fromtimestamp(time.time())     # 时间戳转datetime.datetime类型的本地时间
a_datetime_utc = datetime.datetime.utcfromtimestamp(time.time())    # 时间戳转datetime.datetime类型的utc时间









